package com.hzzh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hzzh.util.ConnectionFactory;
import com.hzzh.vo.MeetingRoom;

public class MeetingRoomDAO {

	private Connection conn;
	private PreparedStatement ps;
	private ResultSet  rs;

	// 添加一个会议室,status默认为0，表示未被占用。当为1时，表示被占用
	public void insert(MeetingRoom meetingroom) {
		conn = ConnectionFactory.getConnection();
		String sql = "insert into meetingroom" + "(roomnum,roomname,capacity,status,description)"
				+ " values(?,?,?,?,?)";
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, meetingroom.getRoomnum());
			ps.setString(2, meetingroom.getRoomname());
			ps.setInt(3, meetingroom.getCapacity());
			ps.setString(4, meetingroom.getStatus());
			ps.setString(5, meetingroom.getDescription());

			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection(rs, ps, conn);
		}
	}

	// 根据ID查询会议室
	public MeetingRoom selectByRoomid(int roomid) {
		conn = ConnectionFactory.getConnection();
		MeetingRoom meetingroom = null;
		try {
			String sql = "select * from meetingroom where roomid=" + roomid + "";
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery(sql);
			if (rs.next() == true) {
				meetingroom = new MeetingRoom();
				meetingroom.setRoomid(Integer.parseInt(rs.getString("roomid")));
				meetingroom.setRoomnum(Integer.parseInt(rs.getString("roomnum")));
				meetingroom.setCapacity(Integer.parseInt(rs.getString("capacity")));
				meetingroom.setRoomname(rs.getString("roomname"));
				meetingroom.setStatus(rs.getString("status"));
				meetingroom.setDescription(rs.getString("description"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection(rs, ps, conn);
		}
		return meetingroom;
	}

	// 查询所有会议室
	public List<MeetingRoom> selectAllMeetingRooms() {
		conn = ConnectionFactory.getConnection();
		List<MeetingRoom> list = new ArrayList<MeetingRoom>();
		MeetingRoom meetingroom = null;
		try {
			String sql = "select * from meetingroom ";
			ps = conn.prepareStatement(sql);
			ResultSet rs = ps.executeQuery(sql);
			while (rs.next()) {
				meetingroom = new MeetingRoom();
				meetingroom.setRoomid(Integer.parseInt(rs.getString("roomid")));
				meetingroom.setRoomnum(Integer.parseInt(rs.getString("roomnum")));
				meetingroom.setCapacity(Integer.parseInt(rs.getString("capacity")));
				meetingroom.setRoomname(rs.getString("roomname"));
				meetingroom.setStatus(rs.getString("status"));
				meetingroom.setDescription(rs.getString("description"));

				list.add(meetingroom);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection(rs, ps, conn);
		}
		return list;
	}

	// 根据ID更新会议室
	public void updateMeetingroom(MeetingRoom meetingroom) {
		conn = ConnectionFactory.getConnection();
		String sql = "update meetingroom set roomnum=?,roomname=?,capacity=?,status=?,description=? where roomid="
				+ meetingroom.getRoomid();
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, meetingroom.getRoomnum());
			ps.setString(2, meetingroom.getRoomname());
			ps.setInt(3, meetingroom.getCapacity());
			ps.setString(4, meetingroom.getStatus());
			ps.setString(5, meetingroom.getDescription());
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection(rs, ps, conn);
		}
	}

	// 根据ID更新会议室状态
	public void updateMeetingroomStatus(int roomid, String status) {
		conn = ConnectionFactory.getConnection();
		String sql = "update meetingroom set status=? where roomid=" + roomid;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, status);
			ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			ConnectionFactory.closeConnection(rs, ps, conn);
		}
	}

	public static void main(String[] args) {
		MeetingRoomDAO mrd = new MeetingRoomDAO();
		MeetingRoom mr = mrd.selectByRoomid(8);
		System.out.println(mr);
	}
	
}
